﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace MusicLine
{
    internal class Cuts : AbstractDAL
    {
        #region Read methods
        internal static DataSet GetAllCutsAndAlbums()
        {
            DataSet CutsAndAlbums = GetAllCuts();
            SqlDataAdapter LookupSqlAdapter = new SqlDataAdapter("Exec ListAllAlbums", GetConnection());
            LookupSqlAdapter.Fill(CutsAndAlbums, "Albums");

            CutsAndAlbums.Relations.Add("Albums_Cuts", CutsAndAlbums.Tables["Albums"].Columns["AlbumId"], CutsAndAlbums.Tables["Cuts"].Columns["AlbumId"]);

            return CutsAndAlbums;
        } // end of GetAllCutsAndAlbums()

        internal static DataSet GetAllCuts()
        {
            SqlDataAdapter LookupSqlAdapter = new SqlDataAdapter("Exec ListAllCuts", GetConnection());

            DataSet Cuts = new DataSet();
            LookupSqlAdapter.Fill(Cuts, "Cuts");
            return Cuts;
        } // end of GetAllCuts()

        internal static DataSet LookupCuts(String Title)
        {
            SqlDataAdapter LookupSqlAdapter = new SqlDataAdapter("Exec LookupCuts @Title", GetConnection());

            LookupSqlAdapter.SelectCommand.Parameters.Add("@Title", System.Data.SqlDbType.VarChar).Value = Title;

            DataSet Cuts = new DataSet();
            LookupSqlAdapter.Fill(Cuts, "Cuts");
            return Cuts;
        } // end of LookupCuts()

        public static DataSet LookupCutsByArtist(string artistName)
        {
            SqlDataAdapter LookupSqlAdapter = new SqlDataAdapter("Exec LookupCutsByArtist @ArtistName", GetConnection());

            LookupSqlAdapter.SelectCommand.Parameters.Add("@ArtistName", System.Data.SqlDbType.VarChar).Value = artistName;

            DataSet Cuts = new DataSet();
            LookupSqlAdapter.Fill(Cuts, "Cuts");
            return Cuts;
        } // end of LookupCutsByArtist()
        #endregion

        #region Update methods
        // TODO: Fix by adding the AlbumId (to uniquely identify the cut)
        public static void Update(int albumId, int cutNumber, string title, int lengthInSeconds)
        {
            SqlCommand updateSqlAdapter = new SqlCommand("EXEC UpdateCut @AlbumId, @CutNumber, @Title, @LengthInSeconds", new SqlConnection(ConnectionString));
            updateSqlAdapter.Parameters.Add("@AlbumId", SqlDbType.Int).Value = albumId;
            updateSqlAdapter.Parameters.Add("@CutNumber", SqlDbType.Int).Value = cutNumber;
            updateSqlAdapter.Parameters.Add("@Title", SqlDbType.VarChar).Value = title;
            updateSqlAdapter.Parameters.Add("@lengthInSeconds", SqlDbType.Int).Value = lengthInSeconds;

            updateSqlAdapter.Connection.Open();
            updateSqlAdapter.ExecuteNonQuery();
        }
        #endregion
    } // end of class
} // end of namespace
